#ifndef     __TIMER_H
#define     __TIMER_H

#include "../rtx/rtx_os.h"
#include "../utils/nocopyable.h"

namespace rtos {

template <typename FUNC, osTimerType_t TYPE = osTimerOnce>
class Timer : private utils::NoCopyable {

public:
    void init(const char *name = "other") {
        osTimerAttr_t attr;
        attr.name = name;
        attr.cb_mem = m_cb_mem;
        attr.cb_size = osRtxTimerCbSize;
        attr.attr_bits = 0;
        m_id = osTimerNew(&Timer::timerFunc, TYPE, this, &attr);
    }

    osStatus_t start(uint32_t ms) {
        osTimerStop(m_id);
        return osTimerStart(m_id, ms);
    }

    osStatus_t stop() {
        return osTimerStop(m_id);
    }

private:
    static void timerFunc(void *args) {
        static_cast<FUNC *>(args)->on_timeout();
    }

private:
    osTimerId_t m_id;
    uint64_t m_cb_mem[ (osRtxTimerCbSize + 7) / 8];
};

}

#endif
